//
//  HomePageVc.m
//  ZITHER
//
//  Created by weixin on 2018/10/10.
//  Copyright © 2018年 weixn. All rights reserved.
//


#import "MainFirst_WX.h"
#import "MainFirst_MagazineList_TBV_WX.h"

#import "MainFirst_Magazine_VC_WX.h"
#import "MainFirst_MagazineVideo_VC_WX.h"
#import "DHGuidePageHUD.h"
#import "MainDiscernSelectWatchVC.h"
#import "MainFirst_ScrollView_WX.h"
#import <MessageUI/MessageUI.h>
#import "UINavigationController+FDFullscreenPopGesture.h"

/** 话题*/
#import "MainTopicViewController.h"

/** 测试控制器 */
#import "Brand_Explore_VC_WX.h"
#import "GoodsDetailsVC.h"
#import "Brands_BrandsDetail_VC_WX.h"
#import "Auction_Main_VC_WX.h"
#import "Auction_SpecialSaleDetails_VC_WX.h"
#import "Auction_taiziselling_VC_WX.h"



/** 活动toast*/
#import "MyQRCodeWebVC.h"
#import "TaskToastView.h"
#import "CRBBoxHomeHttp.h"
//#import "ZYShareView.h"
#import <UShareUI/UShareUI.h>


@interface MainFirst_WX ()<MFMessageComposeViewControllerDelegate>


@property (nonatomic,strong)MainFirst_ScrollView_WX * scroView;

//@property (nonatomic,strong)MainFirst_MagazineList_TBV_WX * tbv;

@property (nonatomic,strong)TaskToastView *taskToasView;

@property (nonatomic,strong)UIView *maskView;

@end

@implementation MainFirst_WX

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self setNav];

    [self setUI];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
        self.view.backgroundColor = Main_Token_Model_MANAGER.backgroundColor ;
    }

    
//    Auction_taiziselling_VC_WX *vc = [[Auction_taiziselling_VC_WX alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];

}

-(void)funcaddBtn{
    BaseBtn * trash = [BaseBtn initCustomBtnImageAndTitles:@[@"",@"已标记不感兴趣"] images:@[@"sku_标记不感兴趣",@""] color:@[HexColor(0x333333),baseColorTheme]];
    [[trash addTag]subscribeNext:^(id  _Nullable x) {
        trash.selected=!trash.selected;
    }];
    [self.view addSubview:trash];
    [trash mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view).offset(0);
        make.width.height.mas_equalTo(100);
    }];
}




-(void)funcIcon{
    BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(100, 100, 64, 64 )];
    view.backgroundColor = baseColorTheme;
    //    [view setCornerRadiusWithRadius:8 isMasks:YES];
    [self.view addSubview:view];
    
    BaseLab *lab = [BaseLab initCustomLab:@"懂表" color:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFang-SC-Regular" size:20]];
    lab.backgroundColor = [UIColor clearColor];
    [view addSubview:lab];
    
    BaseLab *lab1 = [BaseLab initCustomLab:@"帝" color:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFang-SC-Regular" size:15]];
    lab1.backgroundColor = [UIColor clearColor];
    [view addSubview:lab1];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.centerX.equalTo(view.mas_centerX).offset(0);
        make.centerY.equalTo(view.mas_centerY).offset(-10);
    }];
    
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.centerX.equalTo(view.mas_centerX).offset(0);
        make.centerY.equalTo(view.mas_centerY).offset(10);
    }];
    
}
-(void)showMessageViewController:(NSString*)phoneNumber{
    //判断是否能发短息

    if( [MFMessageComposeViewController canSendText] ){
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
        controller.recipients = [NSArray arrayWithObject:phoneNumber];//接收人,可以有很多,放入数组
        controller.body = @"ssssssss";//短信内容,自定义即可
        controller.messageComposeDelegate = self;//注意不是delegate
        [self presentViewController:controller animated:YES completion:nil];
        

//        [self.navigationController pushViewController:controller animated:YES];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发送短信"];//修改短信界面标题}else{NSLog(@"不能发送短信");
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//  发送短信
//    [self showMessageViewController:@"13927408070"];

    /**
     25800 (未收到报价 有更多货源)
     13091（收到报价 有更多货源）
     2  （接受到报价 未收到报价 有更多货源）
     3359
     */

//
//    NSLog(@"点击");
}


-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    if (MessageComposeResultCancelled == result) {
        [controller dismissViewControllerAnimated:YES completion:nil];
    }else if (MessageComposeResultSent == result) {
        [controller dismissViewControllerAnimated:YES completion:nil];

    }
    NSLog(@"");
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.view bringSubviewToFront:self.baseNavigationView];

//    [self.view bringSubviewToFront:_view];
}

#pragma mark - 设置APP静态图片引导页
- (void)setStaticGuidePage {
    //引导页1 （X）/引导页2（X）/引导页3（X）/引导页4 （X）
    //引导页1  引导页2 引导页3 引导页4
    NSArray *imageNameArray = nil;
    if (Is_Iphone_X_Series) {
        imageNameArray = @[@"引导页1 （X）",@"引导页2（X）",@"引导页3（X）",@"引导页4 （X）"];
    }else{
        imageNameArray = @[@"引导页1",@"引导页2",@"引导页3",@"引导页4"];
    }
    
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:YES];
    guidePage.slideInto = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    guidePage.userInteractionEnabled = YES;
    [guidePage addGestureRecognizer:tap];
    [[tap rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//        NSLog(@"点击了");
        CGPoint point = guidePage.guidePageView.contentOffset;
        if ((imageNameArray.count-1)*kScreenW==point.x) {
            [UIView animateWithDuration:0.3 animations:^{
                [guidePage hideSelf];
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                guidePage.guidePageView.contentOffset =CGPointMake(point.x+kScreenW, 0);
            }];
        }
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:guidePage];
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=NO;
    if (kUSER_isPublishImage.type == YES && !kUSER_isPublishImage.typeClose) {
        kUSER_isPublishImage.hidden = NO;
    }
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (kUSER_isPublishImage.type == YES && !kUSER_isPublishImage.typeClose) {
                kUSER_isPublishImage.hidden = YES;
    }
}



-(void)setNav{
    [self.baseNavigationView setHiddenNav:YES VC:self];
    
    
/**
 2019.3.13
 [self.baseNavigationView setHiddenNav:NO VC:self];
 self.baseNavigationView.leftButton1.hidden = YES;
 BaseImageV * image = [[BaseImageV alloc]initializeFrame:CGRectMake(10,kStatusBarHeight, 236/4.5, 84/4.5) contentMode:2];
 //    image.backgroundColor = [UIColor redColor];
 image.contentMode = UIViewContentModeScaleAspectFit;
 image.centerX = self.view.centerX;
 
 if (Is_Iphone_X_Series) {
 image.centerY = self.self.baseNavigationView.centerY+15;
 }else{
 image.centerY = self.self.baseNavigationView.centerY+7;
 }
 image.image = [UIImage imageNamed:@"文字logo"];
 [self.baseNavigationView  addSubview:image];
 */

}

-(void)loaddata{
    if (kUSER_INFO_MANAGER.userIsLogin) {
        [kUSER_INFO_MANAGER setCookie];
    }
}

-(void)setUI{
    /**
     2019.3.13
     [self.view addSubview:self.tbv];
     self.tbv.backgroundColor = [UIColor randomColor];
     [self.tbv mas_makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(self.view).offset(NaviHeight);
     make.left.right.equalTo(self.view).offset(0);
     make.bottom.equalTo(self.view).offset(-50-kBottomSafeHeight);
     }];
     
     //发布提示
     if (!kUSER_INFO_MANAGER.isPublish) {
     kUSER_isPublishImage.hidden = NO;
     kUSER_isPublishImage.type = YES;
     [[UIApplication sharedApplication].keyWindow addSubview:kUSER_isPublishImage];
     }
     */
    
    self.view.backgroundColor = HexColor(0xf5f5f5);

    BaseView *bgView = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        bgView.backgroundColor = Main_Token_Model_MANAGER.backgroundColor ;
    }else{
        bgView.backgroundColor = [UIColor whiteColor];
        if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
            [self setStaticGuidePage];
        }
    }
    [self.view addSubview:bgView];

    

    [self.view addSubview:self.scroView];
    [self.view addSubview:self.TitleV];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.TitleV.mas_bottom).offset(0);
    }];
    
    
    [self.scroView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TitleV.mas_bottom).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-TabbarHeight);
    }];


}


-(Main_Title_View_WX *)TitleV{
    if (!_TitleV) {
        _TitleV = [[Main_Title_View_WX alloc]initWithFrame:CGRectMake(0,kStatusBarHeight, SCREEN_WIDTH, 40 )];
        [_TitleV setShadowType1WithColor:[UIColor defaultShadowColor] offset:CGSizeMake(0, 5) opacity:1 radius:3];
        
        NSMutableArray *arrMNormal = [[NSMutableArray alloc]initWithArray:@[@"推荐",@"新鲜"]];
        _TitleV.arrMNormal = arrMNormal;
        _TitleV.state = MainTitleVStateRight;
        
        
        @weakify(self);
        [_TitleV setBlcokIndex:^(NSInteger index) {
            @strongify(self);
            self.scroView.contentOffset = CGPointMake((index)*kScreenW, 0);
        }];
    }
    return _TitleV;
}

//-(MainFirst_MagazineList_TBV_WX *)tbv{
//    if (!_tbv) {
//         _tbv = [[MainFirst_MagazineList_TBV_WX alloc]initializeFrame:CGRectMake(0, 0, kScreenW, kScreenH) isMj:1 isEstimatedRowHeight:YES addRegisterNib:@[@"MainFirst_MagazineList_UgcCell_WX",@"MainFirst_MagazineList_PGCCell_WX"]];
////        _tbv.backgroundColor = [UIColor randomColor];
//    }
//    return _tbv;
//}

#pragma mark - taskToast
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [UIView new];
        _maskView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    }
    return _maskView;
}

- (TaskToastView *)taskToasView {
    __weak __typeof(self)weakSelf = self;
    if (!_taskToasView) {
        _taskToasView = [[NSBundle mainBundle]loadNibNamed:@"TaskToastView" owner:nil options:nil][0];
        _taskToasView.pushWebBlock = ^{
            MyQRCodeWebVC * vc = [[MyQRCodeWebVC alloc]init];
            vc.navStyle = 1;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            vc.url = [NSString stringWithFormat:@"%@/#/activity/merchant",RootUrlWeb];
        };
        _taskToasView.cancelBlock = ^{
            if (weakSelf.taskToasView) {
                [weakSelf.taskToasView removeFromSuperview];
                weakSelf.taskToasView = nil;
            }
            if (weakSelf.maskView) {
                [weakSelf.maskView removeFromSuperview];
                weakSelf.maskView = nil;
            }
        };
    }
    return _taskToasView;
}

- (void)showTaskToast:(NSString *)userId {
    if (!_taskToasView) {
        [CRBBoxHomeHttp request7daysTaskWithUserId:userId success:^(NSURLSessionDataTask *task, id responseObject) {
            TaskModel *model = (TaskModel *)responseObject;
            
            if (!model.isShow) {
                if ([model.step isEqualToString:model.totalStepCount]) {
                    [self.view addSubview:self.maskView];
                    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.edges.equalTo(self.view);
                    }];
                    [self.maskView addSubview:self.taskToasView];
                    [self.taskToasView setData:responseObject];
                    [self.taskToasView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.size.mas_equalTo(CGSizeMake(258, 416));
                        make.centerX.equalTo(self.maskView);
                        make.centerY.equalTo(self.maskView);
                    }];
                    
                    [CRBBoxHomeHttp requestIsShowTaskToast];
                }
            }

            

        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
            [Tools showTips:msg];
        }];
        
    }
}


-(MainFirst_ScrollView_WX *)scroView{
        if (!_scroView) {
            _scroView = [[MainFirst_ScrollView_WX alloc]initWithFrame:CGRectMake(0, 40+44, kScreenW, 0)];
        }
        return  _scroView;
}
@end
